package com.inhim.pjhim.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.adapter.ChapterAdapter;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.BToast;

import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Request;

public class ChapterActivity extends BaseActivity {
    private YCRefreshView ycRefreshView;
    private List<ReaderTypeList.List> typeList;
    private ChapterAdapter mAdapter;
    private TextView tvCourse;
    private int mPageNum = 1;
    private int totalPage;
    private Boolean refresh = true;
    private String type;

    @Override
    public Object offerLayout() {
        return R.layout.activity_project;
    }

    @Override
    public void onBindView() {
        ycRefreshView = findViewById(R.id.ycRefreshView);
        tvCourse = findViewById(R.id.tvCourse);
        type = getIntent().getStringExtra("type");
        if (type.equals("1")) {
            tvCourse.setText("阅读分类");
        } else if (type.equals("2")) {
            tvCourse.setText("视频分类");
        } else if (type.equals("3")) {
            tvCourse.setText("音频分类");
        }

        ImageView iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        typeList = new ArrayList();
        initAdapter();
        getReaderTypeList();
    }

    @Override
    public void destory() {

    }

    private void initAdapter() {
        typeList = new ArrayList();
        mAdapter = new ChapterAdapter(ChapterActivity.this);
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(ChapterActivity.this, 4);
        ycRefreshView.setLayoutManager(gridLayoutManager);
        //ycRefreshView.setEmptyView(R.layout.empty_view_layout);
        ycRefreshView.setAdapter(mAdapter);
        //设置上拉加载更多时布局，以及监听事件
        mAdapter.setMore(R.layout.view_more, () -> {
            //可以做请求下一页操作
            mPageNum++;
            refresh = false;
            getReaderTypeList();
        });
        ycRefreshView.setRefreshListener(() -> {
            mPageNum = 1;
            refresh = true;
            getReaderTypeList();
        });
        mAdapter.setOnItemClickListener(position -> {
            typeList = mAdapter.getAllData();
            Intent intent = new Intent(ChapterActivity.this, TypeListActivity.class);
            intent.putExtra("ReaderTypeList", typeList.get(position));
            startActivity(intent);
        });
    }

    private void getReaderTypeList() {
        /** {
         "readerStyleId": "string",
         "readerStyleValueId": "string",
         "readerTypeId": "string",
         "title": "string"
         }*/
        HashMap postMap = new HashMap();
        String url = Urls.getReaderTypeList(mPageNum, 10, type,0);
        MyOkHttpClient.getInstance().asyncJsonPost(url, postMap, new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
            }

            @Override
            public void onSuccess(Request request, String result) {
                Gson gson = new Gson();
                ReaderTypeList readerTypeList = gson.fromJson(result, ReaderTypeList.class);
                if (readerTypeList.getCode() == 0 && readerTypeList.getPage().getList().size() > 0) {
                    totalPage = readerTypeList.getPage().getTotalPage();
                    if (refresh) {
                        mAdapter.clear();
                    }
                    for (int i = 0; i < readerTypeList.getPage().getList().size(); i++) {
                        mAdapter.add(readerTypeList.getPage().getList().get(i));
                    }
                    if (mPageNum >= totalPage) {
                        mAdapter.pauseMore();
                    }
                    mAdapter.notifyDataSetChanged();
                } else if (readerTypeList.getCode() != 0) {
                    BToast.showText(readerTypeList.getMsg(), false);
                }
            }
        });
    }

}
